package com.bdqn.controller;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {
    @Resource
    private UserService userService;
    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        User user = userService.getUserById(usrId);
        model.addAttribute("user", user);
        return "demo/hello";
    }

//    @GetMapping(value = "/string")
//    public String getString(Model model) { // 演示赋值，字符串拼接
//        model.addAttribute("userName", "ktjiaoyu");
//        return "demo/string";
//    }

    /**
     * 演示赋值，字符串拼接
     */
    @GetMapping(value = "/string")
    public String getString(Model model, HttpServletRequest request) {
        model.addAttribute("userName", "ktjiaoyu");
        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date", new Date());
        return "demo/string";
    }

    /**
     * 演示内联
     */
    @GetMapping(value = "/inline")
    public String inline(Model model) {
        model.addAttribute("userName", "ktjiaoyu");
        return "demo/inline";
    }

    /**
     * 演示条件判断 if和unless
     */
    @GetMapping(value = "/if")
    public String ifunless(Model model) {
        model.addAttribute("flag", "yes");
        return "demo/if";
    }

    /**
     * 演示for循环
     * switch、case 多用于多条件判断的场景，以性别举例
     */
    @GetMapping(value = "/list")
    public String list(Model model) {
        List<User> list = userService.selectUsersWithRoles();
        model.addAttribute("users", list);
        return "demo/list";
    }
}
